/**
 * @author Tin Pham / 7inpham@gmail.com
 */
Tinz.Monster = function(geometry, texture) {
    var material = new THREE.MeshPhongMaterial({
        map: texture,
        ambient: 0x999999,
        color: 0xffffff,
        specular: 0xffffff,
        shininess: 25,
        morphTargets: true
    });
	var material = new THREE.MeshBasicMaterial({
		wireframe: true,
		color: 0x999999
	});
	var rotationY = new THREE.Matrix4().makeRotationY(Math.PI / 2);
	geometry.applyMatrix(rotationY);
    this.mesh = new THREE.MorphAnimMesh(geometry, material);
    this.mesh.scale.set(4, 4, 4);
	this.mesh.position.y = 90;
    //this.mesh.duration = 1000 * 20;
    this.mesh.castShadow = true;
    this.mesh.receiveShadow = false;
    this.mesh.parseAnimations();// << Funkton fuer Faule P.O.'s :-P 	
    this.mesh.playAnimation ('stand', 7);
    this.direction = {
        left: false,
        backward: false,
        right: false,
        forward: false
    };
    // Set the rays : one vector for every potential direction
    this.rays = [
        new THREE.Vector3(0, 0, 1),
        new THREE.Vector3(1, 0, 1),
        new THREE.Vector3(1, 0, 0),
        new THREE.Vector3(1, 0, -1),
        new THREE.Vector3(0, 0, -1),
        new THREE.Vector3(-1, 0, -1),
        new THREE.Vector3(-1, 0, 0),
        new THREE.Vector3(-1, 0, 1)
    ];
    // And the "RayCaster", able to test for intersections
    this.caster = new THREE.Raycaster();
};

/**
 * Update the direction of the current motion
 */
Tinz.Monster.prototype.setDirection = function(direction) {
    this.direction = direction;
};

/**
 * Update character motions
 */
Tinz.Monster.prototype.update = function(delta) {
    var moveDistance = 400 * delta; // 200 pixels per second
    var rotateAngle = Math.PI / 2 * delta; // pi/2 radians (90 degrees) per second
	//this.mesh.playAnimation ('stand', 7);
    if (this.direction.forward) {
        //this.mesh.position.z += moveDistance;
		this.mesh.translateZ(moveDistance);
        //this.mesh.playAnimation('run', 7);
    }
    if (this.direction.backward) {
        //this.mesh.position.z -= moveDistance;
		this.mesh.translateZ(-moveDistance);
        //this.mesh.playAnimation ('run', 7);
    }
    if (this.direction.left) {
        this.mesh.rotateOnAxis(new THREE.Vector3(0, 1, 0), rotateAngle);
        //this.mesh.playAnimation ('run', 7);
    }
    if (this.direction.right) {
        this.mesh.rotateOnAxis(new THREE.Vector3(0, 1, 0), -rotateAngle);
        //this.mesh.playAnimation ('run', 7);
    }
	this.mesh.updateAnimation(1000 * delta);
};